//滚动 定位 返回
(function($){
	//公开的
	$.fn.backTo = function(options) { 
		var $ele = $(this);//焦点对象
		var settings = {
			sTop:100,//滚动值 显示
			gotobox:'div.gotobox',//定位匹配元素
			icon_li:'li.icon-li',//匹配元素后向追加
			icon_up:'a.icon-arrow-up',//ico up css name
			icon_down:'a.icon-arrow-down',
			topval:50//top修正值 针对元素定位
			};
		settings = $.extend(settings,options);
		
		var goto = $(settings.gotobox);
		var gotolist = '<div>';
		var goto_index = 0;
		var goto_max = parseInt(goto.size())-1;
		var arr_top = new Array();
		goto.each(function(index){
			var top = $(this).offset().top;
			arr_top[index] = [top,$(this).height()];
			gotolist += '<a href="#" top="'+ top +'"><span>'+ $(this).attr('title') +'</span></a>';
		});
		gotolist += '</div>';
		$(settings.icon_li).append(gotolist);//追加定位列表
		$('body').append('<style>.div_shade{ display:none; position:absolute; z-index:99; height:100%; left:0; top:0; width:100%; background-color:#000; height:100%;filter:Alpha(opacity=65);opacity: 0.65;}</style><div class="div_shade"></div>');//遮罩层
		
		var animate = function(index){
			if(goto_max==-1) return;//无滚动定位元素
			//$('a.icon-paragraph-left2').html(index);
			var top = parseInt(goto.eq(index).offset().top)-settings.topval;	
			$("html,body").stop(true,true).animate({ scrollTop: top},500,"swing",function(){goto_index=index;});
		}
		//元素滚动定位
		var up_down = function(index,nodo){
			if(index>goto_max) {index=goto_index=goto_max;}
			if(index<0) {index=goto_index=0;}
			//$('a.icon-paragraph-left2').html(index);
			if(index==0) {
				$(settings.icon_up).click(function(){return false;});	
				$(settings.icon_up).css('color','#999999');
			}else{
				$(settings.icon_up).css('color','#000');
			}
			if(index==goto_max) {
				goto_index=goto_max;
				$(settings.icon_down).click(function(){return false;});	
				$(settings.icon_down).css('color','#999999');
			}else{
				$(settings.icon_down).css('color','#000');
			}
			if(!nodo) animate(goto_index);
		}
		//窗口高度滚动
		var w_index=0;
		var w_max = Math.ceil($(document).height()/$(window).height())-1;
		var up_down_window = function(index,nodo){
			if(index>w_max) {index=w_index=w_max;}
			if(index<0) {index=w_index=0;}
			//$('a.icon-paragraph-left2').html(index);
			if(index==0) {
				$(settings.icon_up).click(function(){return false;});	
				$(settings.icon_up).css('color','#999999');
			}else{
				$(settings.icon_up).css('color','#000');
			}
			if(index==w_max) {
				w_index=w_max;
				$(settings.icon_down).click(function(){return false;});	
				$(settings.icon_down).css('color','#999999');
			}else{
				$(settings.icon_down).css('color','#000');
			}
			if(!nodo) {
				var top = parseInt($(window).height()*w_index);
				if(top>=$(document).height()-$(window).height()) top = $(document).height()-$(window).height();
				$("html,body").stop(true,true).animate({ scrollTop: top},500,"swing",function(){w_index=index;});
			}
		}
		if(goto_max==-1) {
			up_down_window(w_index);
			$(settings.icon_up).click(function(){
				w_index--;
				up_down_window(w_index);
				return false;
			});	
			$(settings.icon_down).click(function(){
				w_index++;
				up_down_window(w_index);
				return false;
			});
		}else {
			up_down(goto_index);
			$(settings.icon_up).click(function(){
				goto_index--;
				up_down(goto_index);
				return false;
			});	
			$(settings.icon_down).click(function(){
				goto_index++;
				up_down(goto_index);
				return false;
			});
		}
		
		$(settings.icon_li).find('div a').click(function(){//滚动定位a标签事件
			animate($(this).index());
		});

		$ele.find('.l_hover').each(function(){
			var timeout=100;//防止下拉菜单因事件隐藏
			var closetimer=null;//判断以防菜单闪烁及隐藏
			var div = $(this).find('div');

			$(this).hover(
			function(){
				if(closetimer){window.clearTimeout(closetimer);closetimer = null;}
				div.show();
				//alert(div.height());
				if($(this).hasClass('l_float_li_2')) div.css("top",'-'+parseInt(div.height()/2-22)+'px');
				$('div.div_shade').height($('body').height()).show();
			},function(){
				closetimer = window.setTimeout(function(){
					div.hide();		
					$('div.div_shade').hide();
				}, timeout);
			})
		});
		
		var tmp_st = 0;//临时滚动值。
		set_index = function(x){//计算定位元素当前索引
			tmp_st = x;
			if(goto_max!=-1) {
				var top = arr_top[goto_index][0];

				if(x>=$(document).height()-$(window).height()-60){goto_index=goto_max;up_down(goto_index,true);return}
				if(x<=50){goto_index=0;up_down(goto_index,true);return}
			
				if(x>top && goto_index<goto_max && x>arr_top[goto_index+1][0]) {goto_index++;set_index(x);up_down(goto_index,true);}
				else if(x<top && goto_index>0 && x<arr_top[goto_index-1][0]) {goto_index--;set_index(x);up_down(goto_index,true);}
			}else{
				var top = $(window).height()*w_index;

				if(x>=$(document).height()-$(window).height()-60){w_index=w_max;up_down_window(w_index,true);return}
				if(x<=50){w_index=0;up_down_window(w_index,true);return}
			
				if(x>top && w_index<w_max && x>$(window).height()*(w_index+1)) {w_index++;set_index(x);up_down_window(w_index,true);}
				else if(x<top && w_index>0 && x<$(window).height()*(w_index-1)) {w_index--;set_index(x);up_down_window(w_index,true);}
			}
		}
		//监听滚动
		$backToFun = function() {
			var st = $(document).scrollTop(),
			winh = $(window).height(); if(st > settings.sTop) $ele.show();//显示定位
			if (!window.XMLHttpRequest) {
				$ele.css("top", st + winh - 166);
			}
			if(Math.abs(st-tmp_st)>50) set_index(st);
			//$('a.icon-paragraph-left2').html($(window).height()+'='+st);
		};
		$(window).bind("scroll", $backToFun);
	}
})(jQuery);